package com.yxq.mdt.app.modules.market.presenter;


import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.base.BasePresenter;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.market.bean.MallResponseBean;
import com.yxq.mdt.app.modules.market.model.MarketModel;
import com.yxq.mdt.app.modules.personal.bean.MainPersonTokenBean;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.net.ApiModel;

public class MainMarketPresenter extends BasePresenter<IMainMarketContract.View, MarketModel> implements IMainMarketContract.Presenter {

    public int page = 1;

    public MainMarketPresenter(IMainMarketContract.View view, MarketModel model) {
        super(view, model);
    }


    @Override
    public void getPersonalMyData() {
        MainPersonTokenBean mainPersonTokenBean = Hawk.get(SPKey.SP_KEY_USER_INFO_TOKEN);
        mBaseModel.getPersonalDate(mainPersonTokenBean != null ? mainPersonTokenBean.getToken() : "", new ApiModel.CommonCallback<MyPersonalBean>() {
            @Override
            public void onSuccess(MyPersonalBean myPersonalBean) {
                mRootView.onLoadSuccess(myPersonalBean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoadPersonalDataFailed();
            }
        });
    }
    @Override
    public void getMainMarketList() {

        mBaseModel.getMallMarketList(new ApiModel.CommonCallback<MallResponseBean>() {
            @Override
            public void onSuccess(MallResponseBean beanList) {
                mRootView.onLoadSuccess(beanList);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoadMarketDataFailed();
            }
        });
    }

    @Override
    public void getMainMarketGoodsList() {
        page = 1;
        mBaseModel.getMallMarketGoodsList(1, 0, -1, new ApiModel.CommonCallback<MallGoodsBean>() {
            @Override
            public void onSuccess(MallGoodsBean beanList) {
                mRootView.onLoadGoodsSuccess(beanList);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoadMarketGoodsDataFailed();
            }
        });
    }

    @Override
    public void getMainMarketGoodsMoreList() {
        page++;
        mBaseModel.getMallMarketGoodsList(page, 0, -1, new ApiModel.CommonCallback<MallGoodsBean>() {
            @Override
            public void onSuccess(MallGoodsBean beanList) {
                mRootView.onLoadGoodsMoreSuccess(beanList);
            }

            @Override
            public void onError(String errMsg) {
                page--;
                mRootView.onLoadMarketGoodsMoreDataFailed();
            }
        });
    }

    public void getMainMarketGoodsList(String cat_id) {
        page = 1;
        mBaseModel.getMallMarketGoodsList(cat_id, 1, 0, -1, new ApiModel.CommonCallback<MallGoodsBean>() {
            @Override
            public void onSuccess(MallGoodsBean beanList) {
                mRootView.onLoadGoodsSuccess(beanList);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoadMarketGoodsDataFailed();
            }
        });
    }

    public void getMainMarketGoodsMoreList(String cat_id) {
        page++;
        mBaseModel.getMallMarketGoodsList(cat_id, page, 0, -1, new ApiModel.CommonCallback<MallGoodsBean>() {
            @Override
            public void onSuccess(MallGoodsBean beanList) {
                mRootView.onLoadGoodsMoreSuccess(beanList);
            }

            @Override
            public void onError(String errMsg) {
                page--;
                mRootView.onLoadMarketGoodsMoreDataFailed();
            }
        });
    }
}
